<template>
  <div class="loginContainer">
    <div class="pic"></div>
    <div class="login-card">
      <el-card>
        <div slot="header" class="clearfix">
          <span>登录</span>
        </div>
        <div class="text item">
          <el-form
            :model="formData"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="username">
              <el-input
                type="text"
                v-model="formData.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                v-model="formData.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { login } from "../api/users";

export default {
  data() {
    return {
      formData: {
        username: "",
        password: "",
      },
      rules: {
        // required 选择必填 trigger: 'blur' blur失去焦点
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.formData)
            .then(({ data }) => {
              if (data.code === 200) {
                console.log(data);
                localStorage.setItem("token", data.data.token);
                localStorage.setItem("refreshToken", data.data.refreshToken);
                localStorage.setItem("adminName", data.data.adminName);
                localStorage.setItem("id", data.data.id);
                localStorage.setItem("realname",data.data.realName);
                this.$router.push("/");
              } else {
                this.$message({ type: "error", message: data.msg });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.loginContainer {
  display: flex;
  align-items: center;
  justify-content: center;
  /* background-color: #eeeeee; */
  height: 100vh;
}
.pic {
  width: 100%;
  height: 100%;
  background: url("../assets/txzz.jpg") no-repeat;
  background-size: cover;
  position: absolute;
  left: 0px;
  top: 0px;
  background-size: cover;
  -webkit-filter: blur(3px);
  -moz-filter: blur(3px);
  -o-filter: blur(3px);
  -ms-filter: blur(3px);
  filter: blur(3px);
}

.el-card.is-always-shadow {
  align-items: center;
  width: 30rem;
  height: 20rem;
}

.el-card {
  border: 1px solid #ebeef5;
  background-color: rgba(134, 130, 130, 0.7);
  color: white;
  transition: 0.3s;
}

.el-form-item__label {
  color: white;
  font-size: 20px;
}

.el-input__inner {
  /* background: transparent; */
  color: white;
  opacity: 0.5;
}

.clearfix {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 35px;
}
.el-form-item__content {
  display: flex;
  justify-content: flex-end;
}

.login-card {
  position: absolute;
  z-index: 5;
}
</style>
<style>
.login-card .el-input__inner {
  background: transparent;
  color: white;
}
.login-card .el-form-item__label {
  color: white;
}
</style>